﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using HPAGMRestAPIWrapper;

namespace AGMLite.BaseClass
{
    public class AGMLiteEntityField : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;        

        #region Private members       
        private AGMField _field;
        private List<AGMField> _availableFields;
        private Dictionary<string, string> _availableFieldOptions;
        #endregion

        #region Properties
        public List<string> AvailableFieldTypes { get; set; }
        public Dictionary<string, List<AGMField>> AvailableFieldsPool { get; set; }
        public List<AGMField> AvailableFields
        {
            get
            {
                return _availableFields;
            }
            set
            {
                if (value != _availableFields)
                {
                    _availableFields = value;
                    OnPropertyChanged("AvailableFields");
                }
            }
        }

        public Dictionary<string, string> AvailableFieldOptions
        {
            get
            {
                return _availableFieldOptions;
            }
            set
            {
                if (value != _availableFieldOptions)
                {
                    _availableFieldOptions = value;
                    OnPropertyChanged("AvailableFieldOptions");
                }
            }
        }

        public AGMField Field
        {
            get
            {
                return _field;
            }
            set
            {
                if (value != _field)
                {
                    _field = value;
                    OnPropertyChanged("Field");
                }
            }
        }
        #endregion

        public AGMLiteEntityField()
        {
            Field = new AGMField();
            Field.PropertyChanged += new PropertyChangedEventHandler(Field_PropertyChanged);
        }

        void Field_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName.Equals("Entity"))
            {
                if (AvailableFieldsPool != null)
                {
                    AvailableFields = AvailableFieldsPool[Field.Entity];
                    AvailableFieldOptions = new Dictionary<string, string>();
                }
            }

            if (e.PropertyName.Equals("Name"))
            {
                if (AvailableFieldsPool != null && !string.IsNullOrEmpty(Field.Name))
                {
                    AGMField tempfield = AvailableFields.Where(o => o.Name.Equals(Field.Name)).Single() as AGMField;
                    AvailableFieldOptions = tempfield.GetOptions();
                    Field.Options = AvailableFieldOptions;
                }
            }
        }

        #region INotifyPropertyChanged Members
        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }
}
